package reflectCus;

import java.lang.reflect.*;

//暴力发射,私有方法
public class Test {
    public static void main(String[] args) throws Exception ,IllegalMonitorStateException{

        try {
            Class cls=Class.forName("reflectCus.Person");
             Constructor c =cls.getConstructor();

            Object o=c.newInstance();
          //  System.out.println(o);
//            Method[] methods=cls.getMethods();
//            for (int i=0;i<methods.length;i++){
//                if(methods[i].getParameterCount()==0){
//                    System.out.println(methods[i]);
//                    methods[i].invoke(o);
//                }
//            }
            Field f=cls.getDeclaredField("name");
            f.setAccessible(true);
            f.set(o,"呵呵");
            System.out.println(o.toString());
            Method[] methods2=cls.getDeclaredMethods();
            for (int i=0;i<methods2.length;i++){
//                methods2[i].getModifiers()== Modifier.PUBLIC
                if(methods2[i].getParameterCount()==0){
                    System.out.println(methods2[i]);
                    methods2[i].setAccessible(true);
                    methods2[i].invoke(o);
                }
            }
        } catch (ClassNotFoundException e) {
           // e.printStackTrace();
        } catch (InstantiationException e) {
           // e.printStackTrace();
        } catch (IllegalAccessException e) {
           // e.printStackTrace();
        } catch (SecurityException e) {
           // e.printStackTrace();
        } catch (IllegalArgumentException e) {
           // e.printStackTrace();
        } catch (InvocationTargetException e) {
           // e.printStackTrace();
        }

    }
    public void sport(){
        System.out.println(getClass().getName()+":运动");
    }
}
